package cn.wolfcode.json;

import cn.wolfcode.domain.Department;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

/*
*  Jackson Spring MVC
*  Fastjson 阿里
* */


public class JsonTest {

    @Test
    public void testJson(){
        String jsonStr = "{\"id\":1,\"name\":\"开发部\",\"sn\":\"dev\"}";
    }

    @Test
    public void testJackson()throws Exception{
        Department department = new Department();
        department.setId(1L);
        department.setName("zzzz");
        department.setSn("DEV");

        ObjectMapper objectMapper = new ObjectMapper();
        //java 对象 -》 json
        System.out.println(objectMapper.writeValueAsString(department));
        //json -> java
        String jsonStr = "{\"id\":1,\"name\":\"开发部\",\"sn\":\"dev\"}";
        System.out.println(objectMapper.readValue(jsonStr,Department.class));
    }

    @Test
    public void testFasytJackson()throws Exception{
        Department department = new Department();
        department.setId(2L);
        department.setName("zxx");
        department.setSn("pro");

        //java 对象 -》 json
        System.out.println(JSON.toJSONString(department));
        //json -> java
        String jsonStr = "{\"id\":2,\"name\":\"ccc\",\"sn\":\"pro\"}";
        System.out.println(JSON.parseObject(jsonStr,Department.class));
    }
}
